<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="database_layer.*" %>
<%@ page import="java.util.*"%>
<%@ page import="processing_layer.*" %>
<%@ page import="java.io.*" %>
<%@ page import="interface_layer.ErrorHandler" %>
<%@ page errorPage="exceptionPage.jsp" %>
<%-- 
	This is the user page, one of the most "full" pages in the web application.
	It contains the user details, along with options for the various viewers of the
	page. The options vary if you're an administrator or just a bypasser (student),
	or if you're looking at your own profile page.
 --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255"/>
<link href="css/templatemo_style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/XSLHandling.js"></script>
<title>User page</title>
</head>
<body>
<jsp:include page="header_part_1.html"/>
            <div id="templatemo_menu">
                <ul>
                    <li><a href="index.jsp">Home</a></li>
                    <li><a href="about.jsp">About Us</a></li>
<%
                    	String username = (String) session.getAttribute("username");
                        if (null != username)
                        {
%>
                            <li><a href="userPage.jsp" class="current">Your profile</a></li>
							<li><a href="allCourses.jsp">Courses</a></li>
                    		<li><a href="allUsers.jsp">Users</a></li>
<%
                        }
%>
                </ul>    	
            </div> <!-- end of templatemo_menu -->
<jsp:include page="header_part_2.html"/>
<div id="content">
<% 
	String viewedUser = request.getParameter("user");
	String userType = (String) session.getAttribute("userType");
	
	if (null == username)
	{
		ErrorHandler.parseForErrors(Common.UNAUTHORIZED_OPERATION, response, null);
		return;
	}
	else
	{
		User userDetails;
		if (null == viewedUser)
		{
			userDetails = QueryDispatcher.getUserDetails(username);
		}
		else
		{
			userDetails = QueryDispatcher.getUserDetails(viewedUser);
		}
		if (null == userDetails)
		{
    		ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
    		return;
    	}
    	else if (null == userDetails.username)
    	{
    		ErrorHandler.parseForErrors(Common.ENTRY_DOESNT_EXIST, response, null);
    		return;    		
	   	}
		else
		{
		    if (null == userDetails.photo)
		    {
%>
            	<img src="images/no-image.gif" style="height:150px; width:150px"/>            		
<%
		    }
		    else
		    {
%>
		    	<img src="UserImage?user=<%= userDetails.username %>" style="height:150px; width:150px"/>
<%
		    }
%>
			<table>
				<tr>
					<td width="%70" id="userPageDetails">
						
<%
			out.println("<b><u>Username</u></b>:&nbsp;&nbsp;" + userDetails.username + "<br/>");
			if (userDetails.isAdmin)
			{
				out.println("<b><u>Role</u></b>:&nbsp;&nbsp; Administrator<br/>");
			}
			else
			{
				out.println("<b><u>Role</u></b>:&nbsp;&nbsp; Student<br/>");
				float takenCreditPoints = QueryDispatcher.getUserCreditPoints(userDetails.username);
				if (-1 == takenCreditPoints)
				{
					// DB error...
					ErrorHandler.parseForErrors(Common.INTERNAL_DB_ERROR, response, null);
					return;
				}
				out.println("<b><u>Taken credit points</u></b>:&nbsp;&nbsp;" + takenCreditPoints + "<br/>");
			}
			out.println("<b><u>First name</u></b>:&nbsp;&nbsp;" + userDetails.firstName + "<br/>");
			out.println("<b><u>Last name</u></b>:&nbsp;&nbsp;" + userDetails.lastName + "<br/>");
			out.println("<b><u>Phone</u></b>:&nbsp;&nbsp;" + userDetails.phone + "<br/>");
			if (null != userDetails.address)
			{
				out.println("<b><u>Address</u></b>:&nbsp;&nbsp;" + userDetails.address + "<br/>");	
			}
%>
					</td>
					<td width="%30">
<%
			// User's page
			if (userDetails.username.equals(username))
			{
%>
                <form method="post" action="PictureUpload" onsubmit="return imageCheck('prof_picture')" enctype="multipart/form-data">
                (<i>Please avoid uploading big pictures...</i>)
                <input type="file" name="prof_picture" id="prof_picture" size="40"/>
                <input type="submit" class="userPageButtons" value="Upload a profile picture" />
                </form>             
                <br/>
                <form method="get" action="updateDetails.jsp">
                <input type="submit" class="userPageButtons" value="Update your details" />
                </form>             
				<br/>
<%
				// update details
				if (userDetails.isAdmin)
				{
%>
					<form method="get" action="newCourse.jsp">
					<input type="submit" class="userPageButtons" value="Add a new course" />
					</form>
					<br/>
					<form method="get" action="adminCreatedCourses.jsp">
					<input type="submit" class="userPageButtons" value="Browse your created courses" />
					</form>
					<br/>			
<%
				}
				else
				{
%>
					<form method="get" action="studentAvailableCourses.jsp">
					<input type="submit" class="userPageButtons" value="View available courses for registration"/>
					</form>
					<br/>
					<form method="get" action="studentRegisteredCourses.jsp">
					<input type="submit" class="userPageButtons" value="View your registered courses"/>
					</form>
					<br/>
					<label for="sheetForm" ><i>Select a style for the time table view</i></label>
					<form name="sheetForm" id="sheetForm" method="post" 
					      action="XSLChooser" enctype="multipart/form-data" onsubmit="return checkTimeTable()">
					<input type="radio" id="XSLToption" name="XSLToption" value="1"/>Stylesheet 1<br/>
					<input type="radio" id="XSLToption" name="XSLToption" value="2"/>Stylesheet 2<br/>
					<input type="radio" id="XSLToption" name="XSLToption" value="3"/>Use your own<br/>
					<input type="file" id="XSLTfile" name="XSLTfile" size="40"/>
					<input type="submit"  class="userPageButtons" value="View your timetable" />
					</form>									
<%
				}
			}

			// Someone else's page
			else
			{
				if (userType.equals(Common.adminType) && false == userDetails.isAdmin)
				{
%>
					<form method="get" action="removeUser">
					<input type="hidden" name="userToRemove" class="userPageButtons" value="<%= userDetails.username %>"/>
					<input type="submit"  class="userPageButtons" value="Remove this user" />
					</form>
<%
				}
				if (userType.equals(Common.adminType))
				{
%>
					<br/>
					<form method="get" action="studentRegisteredCourses.jsp">
					<input type="hidden" name="requestedUser" class="userPageButtons" value="<%= userDetails.username %>"/>
					<input type="submit" class="userPageButtons" value="View user's registered courses"/>
					</form>
<%
				}
			}		
%>
					</td>
				</tr>
			</table>
<%
		}
	}
%>
</div>
<jsp:include page="sidebar.jsp"/>
<jsp:include page="footer.html"/>
</body>
</html>